<?php
//---------------------------------------------------------
//               Buddha Bless, No Bug !
//         User: wuxuehai Date: 2018/8/6 Time: 上午10:20
//---------------------------------------------------------

namespace common\services;


use mdm\admin\components\MenuHelper;
use mdm\admin\models\Menu;
use Yii;
use yii\helpers\Json;

class AdminMenuService extends MenuHelper
{
    /**
     * 获取菜单
     * @param null $root parentId
     * @param bool $refresh
     * @return array
     */
    public static function getAssignedMenus($root = null, $refresh = false)
    {
        $menus = parent::getAssignedMenu(Yii::$app->user->id, null, function ($menu) {
            return static::callBack($menu);
        }, $refresh);

        return $menus;
    }

    protected static function callBack($menu)
    {
        $data = Json::decode($menu['data'], true);
        return [
            'label' => $menu['name'],
            'url' => [$menu['route']],
            'visible' => isset($data['visible']) ? $data['visible'] : true,
            'icon' => $data['icon'] ? $data['icon'] : 'circle-o',
            'include' => isset($data['include']) && is_array($data['include']) ? $data['include'] : [],
            'items' => $menu['children'],
        ];
    }
}
